package com.kingbook.api.rest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kingbook.common.utils.DateUtils;
import com.kingbook.core.annotations.ContainProperties;
import com.kingbook.core.auth.LoginManager;
import com.kingbook.core.result.JSONResult;
import com.kingbook.services.FinanceService;
import com.kingbook.services.SpendService;
import com.kingbook.services.WorkorderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static com.kingbook.common.enums.DateFormatEnum.YEAR_MONTH;
import static com.kingbook.core.auth.LoginManager.AUTH;

/**
 * @Author Bucky
 * @Date 2019-12-08 17:18:13
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("finance")
public class FinanceRest {

    @Autowired
    WorkorderService workorderService;

    @Autowired
    SpendService spendService;

    @Autowired
    FinanceService financeService;

    @PutMapping
    @ContainProperties({"id", "isCustomer", "ownerId", "type", "amount", "confirmAmount", "date"})
    public JSONResult save(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        param.put("mid", LoginManager.getMid(auth));
        log.info("---->{}", param);
        return financeService.updateReceipt(param);
    }

    @DeleteMapping("{id}/{type}")
    public JSONResult remove(@PathVariable int id, @PathVariable int type, @RequestHeader(name = AUTH) String auth) {
        int mid = LoginManager.getMid(auth);
        log.info("---->mid={} id:{} type:{}", mid, id, type);
        return financeService.remove(id, type, mid);
    }

    @GetMapping("{id}")
    public JSONResult find(@PathVariable int id, @RequestHeader(name = AUTH) String auth) {
        int mid = LoginManager.getMid(auth);
        log.info("---->mid={} id={}", mid, id);
        return financeService.find(id, mid);
    }

    @PostMapping("query")
    @ContainProperties({"isCustomer", "ownerId"})
    public JSONResult query(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        log.info("---->{}", param);
        boolean isCustomer = MapUtils.getBoolean(param, "isCustomer");
        int oid = MapUtils.getIntValue(param, "ownerId");
        if (isCustomer)
            param.put("cid", oid);
        else
            param.put("pid", oid);
        param.put("mid", LoginManager.getMid(auth));
        return financeService.query(param);
    }

    @PostMapping("ratio")
    @ContainProperties({"isCustomer", "type"})
    public JSONResult ratio(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        int mid = LoginManager.getMid(auth);
        param.put("mid", mid);
        log.info("---->{}", param);
        boolean isCustomer = MapUtils.getBoolean(param, "isCustomer");
        int type = MapUtils.getIntValue(param, "type");
        if (param.containsKey("ownerId")) {
            int oid = MapUtils.getIntValue(param, "ownerId");
            param.put(isCustomer ? "cid" : "pid", oid);
        }
        JSONResult result;
        if (isCustomer)
            result = workorderService.ratioCustomerReceipt(param);
        else {
            if (type == 1)
                result = workorderService.ratioPartnerReceipt(param);
            else
                result = workorderService.ratioPartnerPay(param);
        }
        result.setMsg(spendService.expendMonth(mid));
        return result;
    }


}
